package com.imooc.o2o.util;

import com.imooc.o2o.dto.ImageHolder;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageUtil {

    private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    private static final SimpleDateFormat sDateFrormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final Random r = new Random();

    public static String generateThumbnail(ImageHolder thumbnail, String targetAddr) {
        // 随机生成文件名
        String realFileName = getRandomFileName();
        // 上传过来的文件的扩展名
        String extension = getFileExtension(thumbnail.getImageName());
        // 创建目标文件夹
        makeDirPath(targetAddr);
        // 拼接文件的相对路径
        String relativeAddr = targetAddr + realFileName + extension;
        // 在拼接的字符串前加上指定的系统文件路径，并生成文件
        File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
        try {
            Thumbnails.of(thumbnail.getImage()).size(1280, 720).outputQuality(0.8f).toFile(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return relativeAddr;
    }


    public static String generateNormalImg(ImageHolder thumbnail, String targetAddr) {
        String realFileName = FileUtil.getRandomFileName();
        String extension = getFileExtension(thumbnail.getImageName());
        makeDirPath(targetAddr);
        String relativeAddr = targetAddr + realFileName + extension;
        File dest = new File(FileUtil.getImgBasePath() + relativeAddr);
        try {
            Thumbnails.of(thumbnail.getImage()).size(337, 640).outputQuality(0.5f).toFile(dest);
        } catch (IOException e) {
            throw new RuntimeException("创建缩略图失败：" + e.toString());
        }
        return relativeAddr;
    }

    /**
     * 创建目标路径所涉及到的所有目录
     *
     * @param targetAddr
     */
    private static void makeDirPath(String targetAddr) {
        // 全路径
        String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
        File dirPath = new File(realFileParentPath);
        if (!dirPath.exists()) {
            // 所有路径上的文件夹递归创建
            dirPath.mkdirs();
        }
    }

    /**
     * 获取文件流的扩展名
     *
     * @param fileName
     * @return
     */
    private static String getFileExtension(String fileName) {
//		String originalFileName = thumbail.getName();
        return fileName.substring(fileName.lastIndexOf("."));
    }

    private static String getRandomFileName() {
        int randomNum = r.nextInt(89999) + 10000;
        String nowTimeStr = sDateFrormat.format(new Date());
        return nowTimeStr + randomNum;
    }

    /**
     * 删除文件或目录
     *
     * @param storePath 目标文件或者目录
     */
    public static void deleteFileOrPath(String storePath) {
        File fileOrPath = new File(PathUtil.getImgBasePath() + storePath);
        if (fileOrPath.exists()) {
            // 目录
            if (fileOrPath.isDirectory()) {
                File file[] = fileOrPath.listFiles();
                for (File file2 : file) {
                    file2.delete();
                }
            }
            //删除
            fileOrPath.delete();
        }
    }

}